package com.bkgy.modular.historicalPlan.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.historicalPlan.entity.AcidRollingHistorcalPlan1720;
import com.bkgy.modular.historicalPlan.entity.ExportAcidRollingHistorcalPlan1720;
import com.bkgy.modular.historicalPlan.mapper.AcidRollingHistorcalPlan1720Mapper;
import com.bkgy.modular.historicalPlan.service.AcidRollingHistorcalPlan1720Service;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName AcidRollingHistorcalPlan1720ServiceImpl
 * @date 2022/12/19 10:08
 */
@Service
public class AcidRollingHistorcalPlan1720ServiceImpl extends ServiceImpl<AcidRollingHistorcalPlan1720Mapper, AcidRollingHistorcalPlan1720> implements AcidRollingHistorcalPlan1720Service {

    @Resource
    private AcidRollingHistorcalPlan1720Mapper acidRollingHistorcalPlan1720Mapper;
    /**
     * 1720酸轧历史计划
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData findByData1720(JSONObject jsonObject) {
        // 获取前端传入的参数
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        JSONObject condition = jsonObject.getJSONObject("condition");



        // 判断前端传入参数pageIndex和pageSize是否为null,为null返回信息
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }

        // 调用Mapper执行sql
        List<AcidRollingHistorcalPlan1720> list = acidRollingHistorcalPlan1720Mapper.findByData1720((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = acidRollingHistorcalPlan1720Mapper.findByCount1720(condition);
        return ResultData.ResultDataSuccess(list, count);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {

        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<AcidRollingHistorcalPlan1720> listExcel = new ArrayList<>();// 调用service查询出数据
        List<AcidRollingHistorcalPlan1720> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = acidRollingHistorcalPlan1720Mapper.findByData1720((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (AcidRollingHistorcalPlan1720 item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportAcidRollingHistorcalPlan1720> list = new ArrayList<>();
        for (AcidRollingHistorcalPlan1720 item : listExcel) {
            ExportAcidRollingHistorcalPlan1720 exportAcidRollingHistorcalPlan1720 = new ExportAcidRollingHistorcalPlan1720();
            BeanUtils.copyProperties(item,exportAcidRollingHistorcalPlan1720);
            list.add(exportAcidRollingHistorcalPlan1720);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\1720酸历史计划.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
